
{hkR1011.i}

DEFINE VARIABLE dept AS CHARACTER.
DEFINE VARIABLE fm   AS CHARACTER.
DEFINE VARIABLE datef AS DATE.
DEFINE VARIABLE datet AS DATE.
DEFINE VARIABLE PRICE LIKE SCT_CST_TOT.
DEFINE VARIABLE vdb   AS CHARACTER.
DEFINE VARIABLE doscmd AS CHARACTER  NO-UNDO.
DEFINE VARIABLE outto AS  CHARACTER INITIAL "R1011SD.csv".

FOR FIRST CODE_mstr NO-LOCK WHERE CODE_fldname = 'HKR1011A'
                              AND CODE_value = "SalseCostRef"
                              AND CODE_cmmt <> "":
END.
IF AVAIL CODE_mstr THEN DO:
    ASSIGN datef = date(integer(substring(code_user1,1,2))
                       ,integer(substring(code_user1,4,2))
                       ,integer(substring(code_user1,7)))
           datet = date(integer(substring(code_user2,1,2))
                       ,integer(substring(code_user2,4,2))
                       ,integer(substring(code_user2,7))).
END.
ELSE DO:
    ASSIGN datef = TODAY - DAY(TODAY) + 1
           datet = TODAY.

END.

for first ad_mstr no-lock where ad_addr = "~~reports".
end.
if avail ad_mstr then do:
  assign vdb = ad_name_control .
end.

FOR EACH code_mstr NO-LOCK WHERE
    code_fldname = "HKR1011A" AND  code_value = "outpath" :
        IF vdb <> "HK" THEN DO:
            ASSIGN outto = code_cmmt + vdb + outto.
        END.
        ELSE DO:
            assign outto  = code_cmmt + outto .
        END.
        doscmd = code_cmmt.
end.

doscmd = "Rar.exe a  -ep -ag -tk " + doscmd + "R1011.rar "
          + doscmd + "R1011*.csv".

OUTPUT TO value(outto).
PUT "fm,dept,date,type,part,qty,price" SKIP.
FOR EACH TR_HIST
    FIELDS(tr_nbr tr_part tr_site tr_effdate tr_qty_loc tr__dec05 tr_type)
    NO-LOCK WHERE tr_effdate >= datef AND tr_effdate <= datet
/*     AND tr_part = '914-2450705R'  */
    AND TR_TYPE = 'ISS-WO':
    ASSIGN fm = "" dept = "".
    FOR FIRST wo_mstr FIELDS(wo_nbr wo_part) NO-LOCK WHERE tr_nbr = wo_nbr:
        FIND FIRST pt_mstr NO-LOCK WHERE pt_part = wo_part NO-ERROR.
        IF AVAIL pt_mstr AND getPtType(pt_prod_line)='fgs' THEN DO:
             FOR FIRST CODE_mstr NO-LOCK WHERE CODE_fldname = 'fmnmref' AND
                      CODE_cmmt =  STRING(YEAR(tr_effdate),"9999") + "N"
                      + STRING(MONTH(tr_effdate),"99"):
                 ASSIGN fm = CODE_value.
             END.
             FOR FIRST pt_mstr NO-LOCK WHERE pt_part = tr_part,
                 FIRST pl_mstr NO-LOCK WHERE pl_prod_line = pt_prod_line:
/*                  MESSAGE pt_prod_line pl_prod_line VIEW-AS ALERT-BOX. */
                 ASSIGN dept = pl__chr02.
             END.
             IF dept <> "" THEN DO:
                 EXPORT DELIMITER "," fm dept string(tr_effdate,"9999-99-99")
                        tr_type tr_part tr_qty_loc * -1 tr__dec05 * 1.17.
             END.
        END.
    END.
END.

FOR EACH tr_hist NO-LOCK WHERE  tr_effdate >= datef AND tr_effdate <= datet
    AND tr_type = "rct-wo",
EACH pt_mstr  FIELDS (pt_part pt_prod_line) NO-LOCK WHERE pt_part = tr_part:
    ASSIGN fm = "" dept = "".
    IF getPtType(pt_prod_line)='fgs' THEN DO:
        FOR FIRST CODE_mstr NO-LOCK WHERE CODE_fldname = 'fmnmref' AND
                  CODE_cmmt =  STRING(YEAR(tr_effdate),"9999") + "N" +
                               STRING(MONTH(tr_effdate),"99"):
            ASSIGN fm = CODE_value.
        END.
        FOR FIRST wo_mstr FIELDS(wo_nbr wo__chr01) NO-LOCK
                  WHERE wo_nbr = tr_nbr :
             IF CAN-FIND(FIRST CODE_mstr NO-LOCK
                  WHERE code_fldname = "hkicpast" AND
                        substring(CODE_value,1,4) = SUBSTRING(wo__chr01,1,4))
             THEN DO:
                FOR FIRST CODE_mstr NO-LOCK
                   WHERE code_fldname = "hkicpast" AND
                   substring(CODE_value,1,4) = SUBSTRING(wo__chr01,1,4):
                    ASSIGN dept = CODE_value.
                END.
            END.
            ELSE IF CAN-FIND(FIRST CODE_mstr NO-LOCK
                 WHERE code_fldname = "hkicpast" AND
                      SUBSTRING (CODE_value,1,3) = SUBSTRING(wo__chr01,1,3))
                 THEN DO:
                FOR FIRST CODE_mstr NO-LOCK
                    WHERE code_fldname = "hkicpast" AND
                    substring(CODE_value,1,3) = SUBSTRING(wo__chr01,1,3):
                   ASSIGN dept = CODE_value.
                END.
            END.
        END.
        EXPORT DELIMITER "," fm dept string(tr_effdate,"9999-99-99") tr_type
                  tr_part  tr_qty_loc tr__dec05 * 1.17 .
    END.
END.

FOR EACH tr_hist NO-LOCK WHERE tr_effdate>=datef AND tr_effdate <= datet
    AND (tr_type = "ISS-SO" OR tr_type = 'iss-prv'),
EACH pt_mstr  FIELDS (pt_part pt_prod_line) NO-LOCK WHERE pt_part = tr_part:
     ASSIGN fm = "" dept = "" PRICE = 0.
     IF getPtType(pt_prod_line)='fgs' THEN DO:
        FOR FIRST CODE_mstr NO-LOCK WHERE CODE_fldname = 'fmnmref' AND
                  CODE_cmmt =  STRING(YEAR(tr_effdate),"9999") + "N" +
                               STRING(MONTH(tr_effdate),"99"):
            ASSIGN fm = CODE_value.
        END.
        FOR FIRST si_mstr NO-LOCK WHERE si_site = tr_site.
            IF si__chr01<> "" THEN DO:
                ASSIGN dept = si__chr01.
            END.
        END.
      run getCost(input "CURRENT", input TR_PART, input tr_site,output price).
        IF tr_qty_loc<>0 AND price<>0 THEN
        EXPORT DELIMITER "," fm dept string(tr_effdate,"9999-99-99") tr_type
                          tr_part  tr_qty_loc * -1 price .
     END.
END.
OUTPUT CLOSE.


QUIT.


